package velocity;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

/**
 * 根据定义模板自动生成代码
 * @author hai
 *
 */
public class Vtemplate {

	public static void main(String[] args) {
		Vtemplate.create("user", "com.abc.a");
	}
	
	/**
	 * 自动生成3层结构代码
	 * @param className  类名
	 * @param pack包绝对路径，如com.dhl.akl
	 */
	public static void create(String className , String pack) {
		 //每一个项目可能路径不一样
		 String rootPath = Vtemplate.class.getClassLoader().getResource("").getFile() + "../src";
		 VelocityEngine ve = new VelocityEngine();
		 ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
		 ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
		 ve.setProperty("input.encoding", "UTF-8");
		 ve.setProperty("output.encoding", "UTF-8");
		 ve.init();
		 String str = className.substring(0,1);
		 String str2 = className.substring(1);
		 String dcla = str.toUpperCase() + str2;//大写类名
		 String xcla = str.toLowerCase() + str2;//小写类名
		 Template t = null;
		 VelocityContext ctx = new VelocityContext();
		 ctx.put("classNameLowCase", xcla);//小写
		 ctx.put("classNameUpCase", dcla);//大写类
		 ctx.put("pack", pack);//包
		//生成dao接口
		 t = ve.getTemplate("template/dao.vm");
		 merge(t,ctx,rootPath+"/"+pack.replace(".", "/")+"/daos/",dcla+"Dao.java");
		 //生成dao实现
		 t = ve.getTemplate("template/daoimp.vm");
		 merge(t,ctx,rootPath+"/"+pack.replace(".", "/")+"/daos/",dcla+"DaoImpl.java");
		//生成service接口
		 t = ve.getTemplate("template/service.vm");
		 merge(t,ctx,rootPath+"/"+pack.replace(".", "/")+"/services/",dcla+"Service.java");
		//生成service实现
		 t = ve.getTemplate("template/serviceimp.vm");
		 merge(t,ctx,rootPath+"/"+pack.replace(".", "/")+"/services/",dcla+"ServiceImpl.java");
		 //生成实体
		 t = ve.getTemplate("template/entity.vm");
		 merge(t,ctx,rootPath+"/"+pack.replace(".", "/")+"/entitys/",dcla+".java");
		 //生成action
		 t = ve.getTemplate("template/action.vm");
		 merge(t,ctx,rootPath+"/"+pack.replace(".", "/")+"/actions/",dcla+"Action.java");
		 //生成xml
		 t = ve.getTemplate("template/xml.vm");
		 merge(t,ctx,rootPath+"/"+pack.replace(".", "/")+"/actions/","struts-"+xcla+".xml");
		 System.out.println("success....");
	}
	
	 private static void merge(Template template, VelocityContext ctx, String path,String fileName) {
		 PrintWriter writer = null;
		 try {
			 File file = new File(path);
			 if(!file.exists()){
				 try {
					 file.mkdirs();
				} catch (Exception e) {
					e.printStackTrace();
				}
			 }
			 File file2 = new File(path+fileName);
			 if(!file2.exists()){
				 try {
					file2.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			 }
		 writer = new PrintWriter(path+fileName);
		 template.merge(ctx, writer);
		 writer.flush();
		 } catch (Exception e) {
			 e.printStackTrace();
		 } finally {
		 writer.close();
		 }
		 }
	 
}
